// Copyright 1998-2019 Epic Games, Inc. All Rights Reserved.

#include "CombineTexture.h"
#include "CombineTextureStyle.h"
#include "CombineTextureCommands.h"
#include "Misc/MessageDialog.h"
#include "Framework/MultiBox/MultiBoxBuilder.h"

#include "SCombineTexture.h"
#include "Widgets/Docking/SDockableTab.h"

#include "LevelEditor.h"

static const FName CombineTextureTabName("CombineTexture");

#define LOCTEXT_NAMESPACE "FCombineTextureModule"

void FCombineTextureModule::StartupModule()
{
	// This code will execute after your module is loaded into memory; the exact timing is specified in the .uplugin file per-module

	FCombineTextureStyle::Initialize();
	FCombineTextureStyle::ReloadTextures();

	FCombineTextureCommands::Register();

	PluginCommands = MakeShareable(new FUICommandList);

	PluginCommands->MapAction(
		FCombineTextureCommands::Get().PluginAction,
		FExecuteAction::CreateRaw(this, &FCombineTextureModule::PluginButtonClicked),
		FCanExecuteAction());

	FLevelEditorModule& LevelEditorModule = FModuleManager::LoadModuleChecked<FLevelEditorModule>("LevelEditor");

	{
		TSharedPtr<FExtender> MenuExtender = MakeShareable(new FExtender());
		MenuExtender->AddMenuExtension("WindowLayout", EExtensionHook::After, PluginCommands, FMenuExtensionDelegate::CreateRaw(this, &FCombineTextureModule::AddMenuExtension));

		LevelEditorModule.GetMenuExtensibilityManager()->AddExtender(MenuExtender);
	}

	{
		TSharedPtr<FExtender> ToolbarExtender = MakeShareable(new FExtender);
		ToolbarExtender->AddToolBarExtension("Settings", EExtensionHook::After, PluginCommands, FToolBarExtensionDelegate::CreateRaw(this, &FCombineTextureModule::AddToolbarExtension));

		LevelEditorModule.GetToolBarExtensibilityManager()->AddExtender(ToolbarExtender);
	}
}

void FCombineTextureModule::ShutdownModule()
{
	// This function may be called during shutdown to clean up your module.  For modules that support dynamic reloading,
	// we call this function before unloading the module.
	FCombineTextureStyle::Shutdown();

	FCombineTextureCommands::Unregister();
}

void FCombineTextureModule::PluginButtonClicked()
{
	FGlobalTabmanager::Get()->RegisterNomadTabSpawner(TEXT("CombineTexture"), FOnSpawnTab::CreateRaw(this, &FCombineTextureModule::OnSpawnPluginTab))
		.SetDisplayName(LOCTEXT("CombineTextureTabTitle", "CombineTexture"))
		.SetMenuType(ETabSpawnerMenuType::Hidden);

	FGlobalTabmanager::Get()->InvokeTab(FName(TEXT("CombineTexture")));
}

void FCombineTextureModule::AddMenuExtension(FMenuBuilder& Builder)
{
	Builder.AddMenuEntry(FCombineTextureCommands::Get().PluginAction);
}

void FCombineTextureModule::AddToolbarExtension(FToolBarBuilder& Builder)
{
	Builder.AddToolBarButton(FCombineTextureCommands::Get().PluginAction);
}


TSharedRef<class SDockTab> FCombineTextureModule::OnSpawnPluginTab(const class FSpawnTabArgs& InSpawnTabArgs)
{
	return SNew(SDockTab)
		.TabRole(ETabRole::NomadTab)
		.Label(LOCTEXT("CombineTextureTab", "Combine Texture"))
		.ToolTipText(LOCTEXT("CombineTextureTabTextToolTip", "Combine Texture"))
		.OnTabClosed(SDockTab::FOnTabClosedCallback::CreateRaw(this, &FCombineTextureModule::OnTabClosed))
		.Clipping(EWidgetClipping::ClipToBounds)
		[
			SNew(SCombineTexture)
		];
}

void FCombineTextureModule::OnTabClosed(TSharedRef<SDockTab> InTab)
{

}

#undef LOCTEXT_NAMESPACE

IMPLEMENT_MODULE(FCombineTextureModule, CombineTexture)


